#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import jinja2

import translate_languages_util
import json
from apiclient.discovery import build
import xml.etree.cElementTree as ET

class MainHandler(webapp2.RequestHandler):
    def get(self):
        # Get parameter from URL
        target_language = self.request.get(translate_languages_util.PARAMETER)

        if target_language == "":
            # Reading *.xml to get content of the file for working with values array
            #  Open file HTML to show HTML page
            TREE = ET.parse(translate_languages_util.XML_FILE)
            root = TREE.getroot()

            for child in root.findall("./" + translate_languages_util.CHILD_IN_FILE + "/[@name='" +
                    translate_languages_util.NAME_IN_CHILD + "']"):
                index_title_page_tag = child[0].tag
                index_title_page = child.find(child[0].tag).text
                index_home_tag = child[1].tag
                index_home = child.find(child[1].tag).text
                index_products_tag = child[2].tag
                index_products = child.find(child[2].tag).text
                index_services_tag = child[3].tag
                index_services = child.find(child[3].tag).text
                index_about_tag = child[4].tag
                index_about = child.find(child[4].tag).text
                index_contact_tag = child[5].tag
                index_contact = child.find(child[5].tag).text

            # Declare values array (object) will include Keys in index.html page
            values = {
                index_title_page_tag : index_title_page,
                index_home_tag : index_home,
                index_products_tag : index_products,
                index_services_tag : index_services,
                index_about_tag : index_about,
                index_contact_tag : index_contact,
                }

            # Declare root path, has included index.html
            templateLoader = jinja2.FileSystemLoader( searchpath="")
            templateEnv = jinja2.Environment( loader=templateLoader)

            # Reading template
            template = templateEnv.get_template(translate_languages_util.HTML_FILE)

            # Output in HTML page
            output = template.render(values)
            self.response.out.write(output)
        else:
            # Reading *.xml to get content of the file for working with values array
            #  Open file HTML to show HTML page
            TREE = ET.parse(translate_languages_util.XML_FILE)
            root = TREE.getroot()

            for child in root.findall("./" + translate_languages_util.CHILD_IN_FILE + "/[@name='" +
                    translate_languages_util.NAME_IN_CHILD + "']"):
                index_title_page_tag = child[0].tag
                index_title_page = child.find(child[0].tag).text
                index_home_tag = child[1].tag
                index_home = child.find(child[1].tag).text
                index_products_tag = child[2].tag
                index_products = child.find(child[2].tag).text
                index_services_tag = child[3].tag
                index_services = child.find(child[3].tag).text
                index_about_tag = child[4].tag
                index_about = child.find(child[4].tag).text
                index_contact_tag = child[5].tag
                index_contact = child.find(child[5].tag).text

            # Declare values array (object) will include Keys in index.html page
            values = {
                index_title_page_tag : index_title_page,
                index_home_tag : index_home,
                index_products_tag : index_products,
                index_services_tag : index_services,
                index_about_tag : index_about,
                index_contact_tag : index_contact,
                }

            # Translated service
            service = build(translate_languages_util.TRANSLATE_API_NAME,
                            translate_languages_util.VERSION,
                            developerKey = translate_languages_util.KEY)

            # TODO q
            # List data after translating by service
            parsed_data = \
                service.translations().list(
                    source = translate_languages_util.SOURCE,
                    target = target_language,
                    q = [index_home, index_products, index_services,
                       index_about, index_contact]
                ).execute()

            # Convert to String
            parsed_data = json.dumps(parsed_data)

            # Convert to JSON Object
            json_object = json.loads(parsed_data)

            # TODO convert do dynamic []array
            # Parse JSON Object
            translatedHome = json_object[translate_languages_util.ROOT][0][translate_languages_util.KEY_PARSE_JSON]
            translatedProducts = json_object[translate_languages_util.ROOT][1][translate_languages_util.KEY_PARSE_JSON]
            translatedServices = json_object[translate_languages_util.ROOT][2][translate_languages_util.KEY_PARSE_JSON]
            translatedAbout = json_object[translate_languages_util.ROOT][3][translate_languages_util.KEY_PARSE_JSON]
            translatedContact = json_object[translate_languages_util.ROOT][4][translate_languages_util.KEY_PARSE_JSON]

            # Write translated language's value for HTML Page in here
            # Declare values array (object) will include Keys in index.html page
            values = {
                index_title_page_tag : index_title_page,
                index_home_tag : translatedHome,
                index_products_tag : translatedProducts,
                index_services_tag : translatedServices,
                index_about_tag : translatedAbout,
                index_contact_tag : translatedContact,
                }

            # Declare root path, has included index.html
            templateLoader = jinja2.FileSystemLoader( searchpath="")
            templateEnv = jinja2.Environment( loader=templateLoader)

            # Reading template
            template = templateEnv.get_template(translate_languages_util.HTML_FILE)

            # Output in HTML page
            output = template.render(values)
            self.response.out.write(output)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)
